package nuaa.service;

import nuaa.dao.IUserAndCollectionDao;
import nuaa.domain.Collection;
import nuaa.util.UtilSqlSession;

import javax.rmi.CORBA.Util;
import java.io.IOException;

import java.util.List;

public class UserAndCollectionService {

    /*dao层对象*/
    IUserAndCollectionDao userAndCollectionDao= UtilSqlSession.getSqlSession().getMapper(IUserAndCollectionDao.class);

    public UserAndCollectionService() throws IOException {
    }

    /**
     * 根据userId查询所有的收藏对象：逆序
     * @param userId
     * @return
     */
    public List<Collection> findAllCollectionsByUserId(Integer userId){
        List<Collection> collections = userAndCollectionDao.findAllCollections(userId);
        return collections;
    }

    /**
     * 根据userId和collectionId删除数据库表记录
     * @param userId
     * @param collectionId
     */
    public void delete(Integer userId,Integer collectionId) throws IOException {
        //1.调用dao层方法删除
        userAndCollectionDao.delete(userId,collectionId);
        //2.即刻提交事务
        UtilSqlSession.getSqlSession().commit();
        //这里即刻查看结果
        List<Collection> collections = userAndCollectionDao.findAllCollections(userId);
        for (Collection collection : collections) {
            System.out.println(collection);
        }
    }
}
